﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using System.DirectoryServices;

//////////////////////////////////////////////////////////////////////////
//CourseInfo.aspx.cs                                                    //
//Last Edited: 23 Mar 2012                                              //
//                                                                      //
//Displays all relevant information for the selected course.            //
//////////////////////////////////////////////////////////////////////////
public partial class Advisee_CourseInfo : System.Web.UI.Page
{
    //////////////////
    //Page Functions//
    //////////////////

    /// <summary>
    /// Page_Load()
    /// 
    /// Initializes the page.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //Get the Course ID from the URL
        String id = Request.QueryString["cid"];
        //If missing, redirect to default
        if (id == null) Response.Redirect("~/Advisee/Default.aspx");
        //Get type of course (i.e. C for course or E for elective)
        char type = id[0];
        id = id.Substring(1);
        if (type == 'C') //View Course Info
        {
            //Get course info from database
            Dictionary<String, object> d1 = StoredProcedures.getCourse(id);
            //If invalid course ID, redirect to default
            if (d1 == null) Response.Redirect("~/Advisee/Default.aspx");
            //Initialize main box controls
            HeaderLabel.Text = "Information for Course " + (String)d1["dpt"] + (String)d1["num"] + ": " + (String)d1["name"];
            CourseInfoBox.Text = "Name: " + (String)d1["name"] + "<br />";
            CourseInfoBox.Text += "Department: " + SharedFunctions.convertFromDptAcronymToString((String)d1["dpt"]) + "<br />";
            CourseInfoBox.Text += "Course Number: " + (String)d1["num"] + "<br />";
            CourseInfoBox.Text += "Credits: " + d1["credits"] + "<br />";
            CourseInfoBox.Text += "<br />";
            //List prerequisites
            List<Dictionary<String, object>> list = StoredProcedures.getCoursePrereqs(id);
            CourseInfoBox.Text += "<strong><u>Prerequisites:</u></strong><br />";
            foreach (Dictionary<String, object> d in list)
            {
                CourseInfoBox.Text += (String)d["dpt"] + (String)d["num"] + ": " + (String)d["name"] + " (" + d["credits"] + ")<br />";
            }
            CourseInfoBox.Text += "<br />";
            //List corequisites
            list = StoredProcedures.getCourseCoreqs(id);
            CourseInfoBox.Text += "<strong><u>Co-requisites:</u></strong><br />";
            foreach (Dictionary<String, object> d in list)
            {
                CourseInfoBox.Text += (String)d["dpt"] + (String)d["num"] + ": " + (String)d["name"] + " (" + d["credits"] + ")<br />";
            }
            CourseInfoBox.Text += "<br />";
        }
        else if(type == 'E') //View Elective Info
        {
            //Get elective info from database
            Dictionary<String, Object> electiveName = StoredProcedures.getElectiveName(id);
            //If invalid elective ID, redirect to default
            if (electiveName == null)
            {
                Response.Redirect("~/Advisee/Default.aspx");
            }
            //List elective info
            HeaderLabel.Text = "Information for Elective: " + (String)electiveName["name"];
            //List choices for elective
            CourseInfoBox.Text = "<strong><u>Courses That Satisfy This Elective:</u></strong><br />";
            List<Dictionary<String, Object>> list = StoredProcedures.getElectiveChoices(id);
            foreach (Dictionary<String, object> d in list)
            {
                CourseInfoBox.Text += (String)d["dpt"] + (String)d["num"] + ": " + (String)d["name"] + " (" + d["credits"] + ")<br />";
            }
        }
        else Response.Redirect("~/Advisee/Default.aspx"); //If invalid type, redirect to default
    } //end Page_Load()

    /// <summary>
    /// CloseButton_Click()
    /// 
    /// Called when user clicks the close button.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void CloseButton_Click(object sender, EventArgs e)
    {
        //Redirect to default
        Response.Redirect("~/Advisee/Default.aspx");
    } //end CloseButton_Click()

    /// <summary>
    /// HelpButton_Click()
    /// 
    /// Called when user clicks the help button.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void HelpButton_Click(object sender, EventArgs e)
    {
        //Redirect to help
        Response.Redirect("~/Advisee/Help.aspx");
    } //end HelpButton_Click()
}